package com.xdu.math.Service;


import com.xdu.math.Dao.UserMapper;
import com.xdu.math.Entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    public String registerUser(UserInfo userInfo){
        if(null != userMapper.getUserInfoByEmail(userInfo.getUserName())){
            return "该邮箱已存在";
        }else{
            userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
            userMapper.insertUserInfo(userInfo);
            return "success";
        }
    }

    public void refreshLastLogin(String userEmail){
        userMapper.refreshLastLogin(userEmail);
    }

    public UserInfo getUserInfoByEmail(String email){
        return userMapper.getUserInfoByEmail(email);
    }

    public String resetPass(UserInfo userInfo){
        if(null ==  userMapper.getUserInfoByEmail(userInfo.getUserEmail())){
            return "不存在该用户，请进入注册界面进行注册";
        }else{
            userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
            userMapper.resetPass(userInfo);
            return "success";
        }
    }
}
